package com.nero.controller;

import com.nero.bean.VO.Slide;
import com.nero.common.utils.HttpClientUtil;
import com.nero.common.utils.JedisClientSingle;
import com.nero.common.utils.SerializeUtils;
import com.nero.service.IContentService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * Created by Nero on 18/8/13.
 */
@Controller
@RequestMapping("/content")
public class ContentController {
    private Logger logger = LogManager.getLogger(this.getClass());
    @Resource
    IContentService contentServiceImpl;

    @Resource
    JedisClientSingle jedisClientSingle;

    @RequestMapping("/getSlides")
    @ResponseBody
    public List<Slide> getSlide() throws IOException, ClassNotFoundException {
//        String str = HttpClientUtil.doGet("http://172.16.16.64:8080/content/slide.do");
//        System.out.println(str);
        List<Slide> list;
        String str = null;
        try {
            str = jedisClientSingle.get("syx_slides");
        } catch (Exception e) {
            list = contentServiceImpl.getSlides();
            return list;
        }
        if (str == null) {
            list = contentServiceImpl.getSlides();
            jedisClientSingle.set("syx_slides", SerializeUtils.serialize(list));
        } else {
            logger.info("从redis取出的轮播图缓存");
            list = (List<Slide>) SerializeUtils.parse(jedisClientSingle.get("syx_slides"));
        }
        return list;
//        return str;
    }
}